package com.hcy.asyncfiledownload.mockdatabase;

import com.hcy.asyncfiledownload.common.entry.ExportRecord;
import com.hcy.asyncfiledownload.datamodule.annotion.ProviderType;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Date 2022/9/5 18:16
 * @Description 模拟数据库操作
 */
public class MockData {

    private static final List<ExportRecord> exportRecords = new ArrayList<>();

    // init
    static {
        ExportRecord exportRecord = new ExportRecord(1L, ProviderType.OrderExport,"{}", 2, "http://localhost/file1","success");
        ExportRecord exportRecord2 = new ExportRecord(2L, ProviderType.OrderExport,"{}",3,"http://localhost/file12", "error");
        exportRecords.add(exportRecord);
        exportRecords.add(exportRecord2);
    }

    public static List<ExportRecord> getExportRecords() {
        return exportRecords;
    }

    public static void insertExportRecord(ExportRecord record) {
        record.setId(exportRecords.size() + 1L);
        exportRecords.add(record);
    }

    public static void updateExportRecord(ExportRecord record) {
        if (record == null || record.getId() == null) {
            return;
        }
        for (ExportRecord next : exportRecords) {
            if (next.getId().equals(record.getId())) {
                BeanUtils.copyProperties(record, next);
            }
        }
    }

    public static ExportRecord getExportRecordById(Long id){
        for (ExportRecord exportRecord : exportRecords) {
            if(exportRecord.getId().equals(id)){
                return exportRecord;
            }
        }
        return null;
    }
}
